package tool.codescan.visitors;

import japa.parser.ast.body.Parameter;
import japa.parser.ast.expr.VariableDeclarationExpr;
import japa.parser.ast.visitor.VoidVisitorAdapter;
import tool.codescan.TypeResolver;
import tool.codescan.loggers.VariableAndParameterLogger;
import tool.data.TypeDefinition;

	/**
	 * @author jwb09119
	 * @date 2013/11/25
	 * 
	 * This visitor is intended to visit a method, specifically to identify method features such as
	 * method parameters and local variables. 
	 */

public class MethodVisitor<A> extends VoidVisitorAdapter<A>{

	private VariableAndParameterLogger storage;
	private IVisitorContext parent;
	
	
	public MethodVisitor(VariableAndParameterLogger logger, IVisitorContext parent) {
		
		storage = logger;
		this.parent = parent;
		
	}
	
	
    @Override
    public void visit(Parameter n, A arg) {
    	//System.out.println("\nVariable Declaration: \n" + n.toString());
    	
    	String name = n.getId().getName();
    	String typeName = TypeResolver.getTypeName(n.getType());
    	TypeDefinition fullTypeName = parent.getFullTypeAddress(typeName);
    	boolean isArray = (n.getId().getArrayCount() > 0);
    	int modifiers = n.getModifiers();
    	
    	storage.registerMethodParameter(name, fullTypeName, isArray, modifiers);

    }
    
    
    @Override
    public void visit(VariableDeclarationExpr n, A arg) {
    	//System.out.println("\nVariable Declaration: \n" + n.toString());
    	
    	String name = n.getVars().get(0).getId().getName();
    	String typeName = TypeResolver.getTypeName(n.getType());
    	TypeDefinition fullTypeName =  parent.getFullTypeAddress(typeName);
    	boolean isArray = ((n.getVars().get(0)).getId().getArrayCount() > 0);
    	int modifiers = n.getModifiers();

    	storage.registerMethodLocalVariable(name, fullTypeName, isArray, modifiers);
    	
    }
	
}
